package com.example.online_exam_system.dao;

import com.example.online_exam_system.entity.StuSub;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * (StuSub)表数据库访问层
 *
 * @author lianglong@asiainfo.com
 * @since 2021-11-25 14:28:45
 */
public interface StuSubDao {

    /**
     * 通过ID查询单条数据
     *
     * @return 实例对象
     */
    StuSub queryById(String userId);

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    List<StuSub> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);


    /**
     * 通过实体作为筛选条件查询
     *
     * @param stuSub 实例对象
     * @return 对象列表
     */
    List<StuSub> queryAll(StuSub stuSub);

    /**
     * 新增数据
     *
     * @param stuSub 实例对象
     * @return 影响行数
     */
    int insert(StuSub stuSub);

    /**
     * 批量新增数据（MyBatis原生foreach方法）
     *
     * @param entities List<StuSub> 实例对象列表
     * @return 影响行数
     */
    int insertBatch(@Param("entities") List<StuSub> entities);

    /**
     * 批量新增或按主键更新数据（MyBatis原生foreach方法）
     *
     * @param entities List<StuSub> 实例对象列表
     * @return 影响行数
     */
    int insertOrUpdateBatch(@Param("entities") List<StuSub> entities);

    /**
     * 修改数据
     *
     * @param stuSub 实例对象
     * @return 影响行数
     */
    int update(StuSub stuSub);

    /**
     * 通过主键删除数据
     *
     * @return 影响行数
     */
    int deleteById();

    @Select("select\n" +
            "        distinct subjectid, studentid, teacherid\n" +
            "        from stu_sub\n" +
            "        where studentid = #{userId}")
    List<Map<String, Object>> getSubjectInfo(@Param("userId")String userId);

    @Select("SELECT DISTINCT s.subjectname,t.realname FROM `subject` s,teacher t ,stu_sub ss \n" +
            "WHERE t.teacherid = ss.`teacherid` \n" +
            "AND ss.studentid = #{userId} \n" +
            "AND ss.subjectid = s.subjectid\n" +
            "AND s.status!=0")
    List<Map<String, Object>> getSubjectInfo2(@Param("userId")String userId);

    @Select("select Distinct subjectid from stu_sub where teacherid = #{teacherid} and studentid=0")
    List<Map<String,Object>>  getTeacherSub(String teacherid);

    @Delete("delete from stu_sub where teacherId= #{param.teacherid} and studentid = 0 and subjectid=#{param.subjectid}")
    int deleteTeaSub(@Param("param")Map<String, Object> param);

    @Insert("insert into stu_sub(teacherid,subjectid,studentid) values(#{param.teacherid},#{param.subjectid},0) ")
    int insertTeaSub(@Param("param") Map<String,Object> param);

    @Select("SELECT DISTINCT t.realname,s.subjectname FROM teacher t,subject s,stu_sub ss\n" +
            "WHERE t.`TEACHERID` = ss.`teacherid`\n" +
            "AND s.`SUBJECTID` = ss.`subjectid`\n" +
            "AND ss.`studentid` =0\n" +
            "AND s.status!=0")
    List<Map<String, Object>> getAllsubtea();

    @Delete("DELETE from stu_sub where studentid = #{inParam.studentid} ")
    int deleteTeaSubByStudentId(@Param("inParam") Map<String, Object> inParam);

    @Insert("Insert into stu_sub(teacherid,subjectid,studentid) values(#{inParam.teacherid},#{inParam.subjectid},#{inParam.studentid})")
    int insert2(@Param("inParam")Map<String, Object> map);

    @Select("SELECT se.testid,se.userid studentid,se.questionid,tq.`stem`,tq.`right`,se.`reply`,tq.score FROM test_questions tq,stu_exam se\n" +
            "WHERE se.`questionid` = tq.`questionid`\n" +
            "AND tq.`type`  <> '单选题' AND se.`score`  IS NULL")
    List<Map<String, Object>> getReadPage();


}
